package com.tourism.website.articlepersonal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tourism.website.articlepersonal.entity.Article;
import com.tourism.website.articlepersonal.entity.NewArticle;
import com.tourism.website.articlepersonal.mapper.ArticleCategoryMapper;
import com.tourism.website.articlepersonal.mapper.ArticleMapper;
import com.tourism.website.articlepersonal.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourismwebsite.articledisplay.dto.ArticleCategoryDTO;
import com.tourismwebsite.articlepersonal.dto.ArticlePersonalDTO;
import com.tourismwebsite.articlepersonal.query.ArticlePersonalQuery;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kevin
 * @since 2024-06-13
 */
@Mapper(componentModel = "spring")
interface MsArticleManageMapper {
    ArticlePersonalDTO ArticleToArticleManageDTO(Article article);
}

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Resource
    MsArticleManageMapper msArticleManageMapper;
    @Resource
    ArticleMapper articleMapper;
    @Resource
    ArticleCategoryMapper articleCategoryMapper;
    //查询文章内容
    @Override
    public List<ArticlePersonalDTO> queryArticlePersonal(String username) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Article::getArticleId, Article::getTitle, Article::getUserName, Article::getNickName,
                Article::getReviewer, Article::getCreateTime, Article::getUpdateTime, Article::getReviewerTime,
                Article::getUserId, Article::getContent,Article::getStatus);
        wrapper.eq(Article::getUserName, username);
        List<Article> articles = baseMapper.selectList(wrapper);
        List<ArticlePersonalDTO> articlePersonalDTOS = articles.stream()
                .map(Article -> msArticleManageMapper.ArticleToArticleManageDTO(Article))
                .collect(Collectors.toList());
        return articlePersonalDTOS;
    }

    @Override
    public Boolean deleteArticleManage(Integer articleId) {
        int result = articleMapper.deleteArticleManage(articleId);
        return result > 0;
    }

    @Override
    public List<ArticlePersonalDTO> queryNameAndTime(Integer articleId) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Article::getUpdateTime, Article::getTitle,Article::getStatus);
        wrapper.eq(Article::getArticleId, articleId);
        List<Article> articles = baseMapper.selectList(wrapper);
        List<ArticlePersonalDTO> articlePersonalDTOS = articles.stream()
                .map(Article -> msArticleManageMapper.ArticleToArticleManageDTO(Article))
                .collect(Collectors.toList());
        return articlePersonalDTOS;
    }
    //发表文章
    @Override
    public boolean saveArticle(ArticlePersonalQuery articlePersonalQuery,Integer categoryId) {
        Article newArticle = new Article();
        newArticle.setTitle(articlePersonalQuery.getTitle());
        newArticle.setContent(articlePersonalQuery.getContent());
        newArticle.setCreateTime(LocalDateTime.now());
        newArticle.setNickName(articlePersonalQuery.getNickName());
        newArticle.setUpdateTime(LocalDateTime.now());
        newArticle.setUserName(articlePersonalQuery.getUserName());
        newArticle.setStatus("2");
        newArticle.setDelFlag("0");
        newArticle.setUserId(articlePersonalQuery.getUserId());


        int count = baseMapper.insert(newArticle);
        if (count > 0) {
            // 获取插入后的主键
            articlePersonalQuery.setArticleId(newArticle.getArticleId());
            return true;
        }
        return false;
    }

    @Override
    public List<ArticleCategoryDTO> getArticlesByUserIdAndCategoryId(Integer userId, Integer categoryId) {
            // 第一步：查询个人的所有 articleId
            List<Integer> articleIds = articleMapper.selectArticleIdsByUserId(userId);

            // 将 articleIds 转换成逗号分隔的字符串，用于 IN 子句
            String articleIdsStr = articleIds.stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));

            // 第二步：根据 categoryId 查询个人的所有文章
            List<ArticleCategoryDTO> articles = articleMapper.selectArticlesByCategoryIdAndArticleIds(categoryId, articleIdsStr);
            return articles;
        }
    }
